package lzupdater.gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.URL;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;

import lzupdater.com.CommunicationHub;
import lzupdater.com.HubComponent;
import lzupdater.gui.elements.LocalizedJButton;

//LZRosterUpdater, a java client to update LZRoster
//Copyright (C) 2008 Fraskor from Krasus (e-mail: fraskor@gmail.com)
//
//This file is part of LZRosterUpdater.
//
//LZRosterUpdater is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

public class AboutFrame extends HubComponent {

	private JFrame frame;
	private MainGui mainGui;
	
	public AboutFrame(CommunicationHub hub) {
		super(hub);
		
		frame = new JFrame() { /**
			 * 
			 */
			private static final long serialVersionUID = -6335310055237266420L;

		@Override
		public void setVisible(boolean flag) {prepareFrame(this, flag); super.setVisible(flag);}};
		
		JPanel panel = new JPanel();
		panel.setLayout(new GridBagLayout());
		
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0; c.gridy = 0;
		c.fill = GridBagConstraints.BOTH;
		c.weightx =1;
		c.weighty =1;
		c.insets = new Insets(5,5,2,5);
		c.anchor = GridBagConstraints.CENTER;		
		
		JEditorPane editorPane = createEditorPane();
		panel.add(editorPane,c);
	
		
		
		c.gridx = 0; c.gridy = 1;
		c.weightx =1;
		c.weighty =0;
		c.fill = GridBagConstraints.VERTICAL;
		c.anchor = GridBagConstraints.CENTER;		
		c.insets = new Insets(5,5,5,5);
		JButton button = createButton();
		panel.add(button,c);

		Dimension dEditor = editorPane.getPreferredSize();
		Dimension dButton = button.getPreferredSize();

		Dimension d = new Dimension(dEditor.width, dEditor.height + dButton.height * 2);
		
		panel.setSize(d);
		frame.getContentPane().add(panel);
		frame.pack();

	}
	
	private JButton createButton() {
		JButton button = new LocalizedJButton("OK");
		button.setAction(new AbstractAction() {
			/**
			 * 
			 */
			private static final long serialVersionUID = -2219905162969520577L;

			public void actionPerformed(ActionEvent arg0) {
				frame.setVisible(false);
			}
		});
		return button;
	}
	
	private JEditorPane createEditorPane() {
		JEditorPane pane = new JEditorPane();
		
		pane.setBackground(frame.getBackground());
		URL urlAbout = AboutFrame.class.getClassLoader().getResource("about/info.html");

		pane.setEditable(false);
		
		if (urlAbout != null) {
			try {
				pane.setPage(urlAbout);
				
			} catch (IOException e) {
				//
			}
		}
		return pane;
	}
	
	public JFrame getGui() {
		return frame;
	}
	
	@Override
	public void initializeCommunication() {
		mainGui = hub.getComponent(MainGui.class);
	}

	public void prepareFrame(JFrame frame, boolean flag) {
		if (flag && !frame.isVisible()) {
			JFrame main = mainGui.getGui();
			Dimension dMain = main.getSize();
			Dimension dAbout = frame.getSize();
			
			int xMain = main.getX();
			int yMain = main.getY();
			
			int xAbout = xMain + (dMain.width - dAbout.width)/2;
			int yAbout = yMain + (dMain.height - dAbout.height)/2;
			
			frame.setLocation(xAbout, yAbout);
			
		}
	}
	
}
